home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / ex / Vehicle.c < prev    next >
C/C++ Source or Header  |  1990-05-15  |  3KB  |  144 lines

  1. // Vehicle.c -- Abstract base class for various kinds of Vehicles
  2.  
  3. #include "Vehicle.h"
  4. #include "AllVehicles.h"
  5. #include "VehicleQ.h"
  6. #include "OrderedCltn.h"
  7. #include "nihclIO.h"
  8. #include "OIOnih.h"
  9. #include <fstream.h>
  10. #include <stdarg.h>
  11.  
  12. #define THIS    Vehicle
  13. #define BASE_CLASSES AllLink::desc(),QLink::desc()
  14. #define MEMBER_CLASSES
  15. #define VIRTUAL_BASE_CLASSES
  16.  
  17. DEFINE_ABSTRACT_CLASS_MI(Vehicle,1,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/Vehicle.c,v 3.0 90/05/15 22:44:08 kgorlen Rel $",NULL,NULL);
  18.  
  19. AllVehicles Vehicle::allVehicles;
  20. IdentSet Vehicle::v;
  21.  
  22. void* Vehicle::_castdown(const Class& target) const
  23. {
  24.     if (&target == desc()) return (void*)this;
  25.     void* p = AllLink::_castdown(target);
  26.     void* q = p;
  27.     if (p = QLink::_castdown(target)) ambigCheck(p,q,target);
  28.     return q;
  29. }
  30.  
  31. Vehicle::Vehicle(float h, float l)
  32. {
  33.     id = allVehicles.size()+1;          // assign an ID number
  34.     height = h; length = l;
  35.     allVehicles.addVehicle(*this);      // add to list of
  36.                                         // all Vehicles
  37. }
  38.  
  39. Vehicle::~Vehicle()
  40. {
  41.     allVehicles.removeVehicle(*this);   // remove from list of
  42.                                         // all Vehicles
  43. }
  44.  
  45. bool Vehicle::operator==(const Vehicle& v) const
  46. {
  47.     return id == v.id;
  48. }
  49.  
  50. int Vehicle::compare(const Object& v) const
  51. {
  52.     assertArgSpecies(v,classDesc,"compare");
  53.     return id - castdown(v).id;
  54. }
  55.  
  56. void Vehicle::deepenShallowCopy()
  57. {
  58.     AllLink::deepenShallowCopy();
  59.     QLink::deepenShallowCopy();
  60.     allVehicles.addVehicle(*this);  // add to list of all Vehicles
  61. }
  62.  
  63. unsigned Vehicle::hash() const  { return id; }
  64.  
  65. bool Vehicle::isEqual(const Object& v) const
  66. {
  67.     return v.isSpecies(classDesc) && *this == castdown(v);
  68. }
  69.  
  70. void Vehicle::_printOn(ostream& strm) const
  71. {
  72.     if (v.includes(*(Object*)(void*)this)) return;
  73.                                       // members already printed
  74.     v.add(*(Object*)(void*)this);
  75.     strm << '#' << id << " height " << height << "  length "
  76.         << length;
  77. }
  78.  
  79. void Vehicle::printOn(ostream& strm) const
  80. {
  81.     v.removeAll();
  82.     _printOn(strm);
  83. }
  84.  
  85. Vehicle::Vehicle(OIOin& strm) :
  86.     Object(strm),
  87.     AllLink(strm),
  88.     QLink(strm)
  89. {
  90.     strm >> id >> height >> length;
  91.     allVehicles.addVehicle(*this);  // add to list of all Vehicles
  92. }
  93.  
  94. void Vehicle::storer(OIOout& strm) const
  95. {
  96.     AllLink::storer(strm);
  97.     QLink::storer(strm);
  98.     strm << id << height << length;
  99. }
  100.  
  101. Vehicle::Vehicle(OIOifd& fd) :
  102.     Object(fd),
  103.     AllLink(fd),
  104.     QLink(fd)
  105. {
  106.     fd >> id >> height >> length;
  107.     allVehicles.addVehicle(*this);  // add to list of all Vehicles
  108. }
  109.  
  110. void Vehicle::storer(OIOofd& fd) const
  111. {
  112.     AllLink::storer(fd);
  113.     QLink::storer(fd);
  114.     fd << id << height << length;
  115. }
  116.  
  117. void Vehicle::printAll(ostream& strm)
  118. {
  119.     allVehicles.printOn(strm);
  120. }
  121.  
  122. #ifdef sparc
  123. // Use magic name to make stdarg work
  124. #define fname __builtin_va_alist
  125. #endif
  126.  
  127. void Vehicle::saveQueues(const char* fname, ...)
  128. {
  129.     ofstream out(fname,ios::out,0664);  // UNIX protection
  130.                                         // mode 0664
  131.     if (out.fail()) {
  132.         cerr << "Failed to open " << fname << endl;
  133.         exit(1);
  134.     }
  135.     OrderedCltn allQueues;
  136.     allQueues.add(allVehicles);     // allVehicles is allQueues[0]
  137.     va_list ap;
  138.     va_start(ap, fname);
  139.     VehicleQ* q;
  140.     while (q = va_arg(ap, VehicleQ*)) allQueues.add(*q);
  141.     va_end(ap);
  142.     allQueues.storeOn(OIOnihout(out));
  143. }
  144.